package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserParam;
import cn.wolfcode.luowowo.domain.*;
import cn.wolfcode.luowowo.query.QueryObject;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.redis.service.IStrategyStatiesVORedisService;
import cn.wolfcode.luowowo.redis.service.impl.StrategyStatiesVORedisServiceImpl;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.util.ParamMap;
import com.sun.org.apache.xml.internal.security.algorithms.implementations.SignatureDSA;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyCatalogService catalogService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService themeService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private StrategyStatiesVORedisServiceImpl strategyStatiesVORedisService;

    //通过目的地的id查攻略的分类
    @GetMapping("/queryCatalogsByDestId")
    public Object queryCatalogsByDestId(String destId) {
        //catalogs
        List<StrategyCatalog> catalogs = catalogService.queryByDestId(destId);
        return JsonResult.success(catalogs);
    }

    //查看攻略详情
    @GetMapping("/detail")
    public Object detail(String id, @UserParam UserInfo userInfo) {
        //strategy
        Strategy strategy = strategyService.get(id);

        //评论的点赞要分页
        StrategyCommentQuery qo = new StrategyCommentQuery();
        qo.setStrategyId(id);
        Page<StrategyComment> page = strategyCommentService.query(qo);

        //访问一次，阅读量就增加一次
        strategyStatiesVORedisService.viewnumIncrease(id, 1);

        //收藏状态的回显  sids  区分开登录和未登录的情况: 因为这个方法没有必须要登录，所以可能有两种情况
        List<String> list = Collections.emptyList();//空list集合
        if (userInfo != null) {
            //登录用户:通过用户的额id去Redis中拿到vo中的sids
            list = strategyStatiesVORedisService.getSids(userInfo.getId());
        }

        //因为这个方法没有必须要登录才可以操作，所以，获取sids的情况分为登录和没登录两种
        List<String> sids = Collections.emptyList();//没有登录sids就是空的
        if (userInfo != null){
            sids = strategyStatiesVORedisService.getSids(userInfo.getId());
        }

        return JsonResult.success(new ParamMap()
                .put("strategy", strategy)
                .put("page", page)
                .put("sids", list)
                .put("vo", strategyStatiesVORedisService.getStrategyStatisVo(id))
                .put("sids", sids)
        );
    }

    //查看攻略详情
    @GetMapping("/list")
    public Object list(StrategyQuery qo) {
        //vue.page = map.page;    //分页对象
        Page<Strategy> page = strategyService.query(qo);
        //vue.themes = map.themes;    //主题
        List<StrategyTheme> themes = themeService.list();
        //vue.toasts = map.toasts;    //吐司
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        return JsonResult.success(new ParamMap()
                .put("page", page)
                .put("themes", themes)
                .put("toasts", toasts)
        );
    }

    //查看某个主题下的所有攻略
    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        //page
        Page<Strategy> page = strategyService.query(qo);
        return JsonResult.success(page);
    }

    //在攻略下添加评论
    @RequireLogin //需要登录才能操作
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo) {
        //设置评论里与用户相关的信息
        BeanUtils.copyProperties(userInfo, comment);
        comment.setUserId(userInfo.getId());//属性名字不同时，单独设置

        //添加评论
        strategyCommentService.addComment(comment);

        //评论的分页
        StrategyCommentQuery qo = new StrategyCommentQuery();
        qo.setStrategyId(comment.getStrategyId());
        Page<StrategyComment> page = strategyCommentService.query(qo);

        //添加一条评论：评论统计 + 1
        strategyStatiesVORedisService.replynumIncrease(comment.getStrategyId(), 1);

        //返回前台的数据
        return JsonResult.success(new ParamMap()
                .put("page", page)
                .put("vo", strategyStatiesVORedisService.getStrategyStatisVo(comment.getStrategyId()))
        );
    }

    //在攻略下的评论上点赞
    @RequireLogin //需要登录才能操作
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, String sid, @UserParam UserInfo userInfo) {

        //点赞操作
        strategyCommentService.commentThumb(cid, sid);

        //评论的点赞也要分页
        StrategyCommentQuery qo = new StrategyCommentQuery();
        qo.setStrategyId(sid);
        Page page = strategyCommentService.query(qo);

        return JsonResult.success(page);
    }

    //在攻略下的收藏
    @RequireLogin //需要登录才能操作
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo) {

        //攻略收藏： ret：true 收藏成功 ，false：取消收藏
        boolean ret = strategyStatiesVORedisService.favor(sid, userInfo.getId());

        List<String> sids = strategyStatiesVORedisService.getSids(userInfo.getId());

        //返回ret结果给前台
        return JsonResult.success(new ParamMap()
                .put("ret", ret)
                .put("sids",sids )
                .put("vo", strategyStatiesVORedisService.getStrategyStatisVo(sid))
        );
    }


    //攻略 顶点赞
    @RequireLogin //需要登录才能操作
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo) {

        //攻略点赞： ret：true 点赞成功 ，false：今天已经点赞过
        boolean ret = strategyStatiesVORedisService.strategyThumbup(sid, userInfo.getId());

        //返回ret结果给前台
        return JsonResult.success(new ParamMap()
                .put("ret", ret)
                .put("vo", strategyStatiesVORedisService.getStrategyStatisVo(sid))
        );
    }




}
